package com.neighbor.neighborhooduser.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neighbor.neighborhooduser.pojo.MallOrders;
import com.neighbor.neighborhooduser.dao.IMallOrdersDao;
import com.neighbor.neighborhooduser.service.IMallOrdersService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 商城兑换订单表 服务实现类
 * </p>
 *
 * @author song
 * @since 2025-09-22 09:13:04
 */
@Service
public class MallOrdersServiceImpl extends ServiceImpl<IMallOrdersDao, MallOrders> implements IMallOrdersService {
    @Override
    public IPage<MallOrders> getOrdersByUserId(Page<MallOrders> page, Integer userId) {
        QueryWrapper<MallOrders> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId)
                .orderByDesc("created_at");
        return this.page(page, wrapper);
    }

    @Override
    public IPage<MallOrders> getOrdersByUserIdAndStatus(Page<MallOrders> page, Integer userId, Integer status) {
        QueryWrapper<MallOrders> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId)
                .eq("status", status)
                .orderByDesc("created_at");
        return this.page(page, wrapper);
    }

    @Override
    public long countOrdersByUserId(Integer userId) {
        QueryWrapper<MallOrders> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        return this.count(wrapper);
    }

    @Override
    public long countOrdersByUserIdAndStatus(Integer userId, Integer status) {
        QueryWrapper<MallOrders> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId)
                .eq("status", status);
        return this.count(wrapper);
    }

    @Override
    public Map<String, Object> getOrderStatsByUserId(Integer userId) {
        Map<String, Object> stats = new HashMap<>();

        // 总订单数
        QueryWrapper<MallOrders> totalWrapper = new QueryWrapper<>();
        totalWrapper.eq("user_id", userId);
        stats.put("total", this.count(totalWrapper));

        // 待领取订单数
        QueryWrapper<MallOrders> pendingWrapper = new QueryWrapper<>();
        pendingWrapper.eq("user_id", userId).eq("status", 1);
        stats.put("pending", this.count(pendingWrapper));

        // 配送中订单数
        QueryWrapper<MallOrders> deliveringWrapper = new QueryWrapper<>();
        deliveringWrapper.eq("user_id", userId).eq("status", 2);
        stats.put("delivering", this.count(deliveringWrapper));

        // 已完成订单数
        QueryWrapper<MallOrders> completedWrapper = new QueryWrapper<>();
        completedWrapper.eq("user_id", userId).eq("status", 3);
        stats.put("completed", this.count(completedWrapper));

        // 已取消订单数
        QueryWrapper<MallOrders> canceledWrapper = new QueryWrapper<>();
        canceledWrapper.eq("user_id", userId).eq("status", 4);
        stats.put("canceled", this.count(canceledWrapper));

        return stats;
    }
}
